package com.yolib.sanyi;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.yolib.sanyi.connection.ConnectionService;
import com.yolib.sanyi.connection.event.NewsEvent;
import com.yolib.sanyi.connection.event.EventHandler;
import com.yolib.sanyi.connection.event.StoresEvent;
import com.yolib.sanyi.connection.event.StoryEvent;
import com.yolib.sanyi.connection.event.TrafficEvent;
import com.yolib.sanyi.connection.event.TravelEvent;
import com.yolib.sanyi.object.NewsObject;
import com.yolib.sanyi.object.StoryPointObject;
import com.yolib.sanyi.object.TrafficObject;
import com.yolib.sanyi.tool.YoliBLog;

import android.R.mipmap;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.text.Html;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class TrafficActivity extends Activity implements OnClickListener{

	ListView mListView;
	NewsAdapter mAdapter;
	ImageView 	mBack;
	
	List<TrafficObject> mTrifficList = new ArrayList<TrafficObject>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_traffic);
		mListView = (ListView) findViewById(R.id.traffic_listview);
		mBack	 = 	(ImageView)  findViewById(R.id.BackImageView);
		mAdapter = new NewsAdapter(TrafficActivity.this);
		TrafficEvent mTrafficEvent = new TrafficEvent(TrafficActivity.this);
		mTrafficEvent.setHandler(mHttpHandler);
		ConnectionService.getInstance().addAction(mTrafficEvent);
		mListView.setAdapter(mAdapter);
		mBack.setOnClickListener(this);
	}

	class NewsAdapter extends BaseAdapter{

		Context mContext;
		LayoutInflater mInflater;
		public NewsAdapter(Context context) {
			mContext = context;
			mInflater = LayoutInflater.from(context);
		}
		
		@Override
		public int getCount() {
			YoliBLog.e("size: "+ mTrifficList.size());
			return mTrifficList.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			ViewHolder holder;

			if (convertView == null || convertView instanceof TextView) {
				convertView = mInflater.inflate(R.layout.view_travel_item, null);

				holder = new ViewHolder();
				holder.content = (TextView) convertView.findViewById(R.id.view_travel_item_title);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			
			String str =  mTrifficList.get(position).tilte;
			Spanned spanned = Html.fromHtml(str);
			holder.content.setText(spanned);
			convertView.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent mIntent = new Intent(mContext, NewsContentActivity.class);
					mIntent.putExtra("content", mTrifficList.get(position).content );
					mIntent.putExtra("title", mTrifficList.get(position).tilte );
					startActivity(mIntent);
					overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
				}
			});
			return convertView;
		}

	}
	
	class ViewHolder{
		TextView content;
		ImageView pic;//�n���picview
	}

	EventHandler mHttpHandler = new EventHandler()
	{
		public void dispatchMessage(Message msg) {
			if(getClassName(msg).compareTo(TrafficEvent.class.getName()) == 0){
				DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

				try {
					DocumentBuilder builder = factory.newDocumentBuilder();
					Document document = builder.parse(new InputSource(new ByteArrayInputStream(((String)msg.obj).getBytes("utf-8"))));
					Element root = document.getDocumentElement();
					NodeList nodes = root.getElementsByTagName("traffic");
					for(int i = 0; i < nodes.getLength(); i++){
						Element newsElement = (Element)nodes.item(i);
						TrafficObject mTrafficObject = new TrafficObject();
						mTrafficObject.setTrafficData(newsElement.getChildNodes());

						mTrifficList.add(mTrafficObject);
					}

				} catch (SAXException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ParserConfigurationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				mAdapter.notifyDataSetChanged();
			}
		}
	};
	
	@Override
	public void onClick(View v) {
		switch(v.getId())
		{
		case R.id.BackImageView:
			finish();
			overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);	
			break;
		}
	}

}
